﻿//---------------------------------------------------------------------------------
// Copyright 2011 Microsoft Corporation
// Licensed under the Microsoft Public License (MS-PL) (the "License"); 
// You may not use this file except in compliance with the License. 
// You may obtain a copy of the License at http://www.opensource.org/licenses/ms-pl.html
// THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR 
// CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, 
// INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR 
// CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, 
// MERCHANTABLITY OR NON-INFRINGEMENT. 
// See the Microsoft Public License (MS-PL) for the specific language governing 
// permissions and limitations under the License.
//---------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace Microsoft.Telco.WindowsLive
{
	public class AppAuthentication
	{

		// Methods
		public AppAuthentication()
		{
		}

		public AppAuthentication(IDictionary<string, string> authInfo)
		{
			if (authInfo == null)
			{
				throw new ArgumentNullException("authInfo");
			}
			foreach (string str in authInfo.Keys)
			{
				string grantedOffers = authInfo[str];
				string str3 = str;
				if (str3 != null)
				{
					if (!(str3 == "AccessToken"))
					{
						if (str3 == "RefreshToken")
						{
							goto Label_0085;
						}
						if (str3 == "Expiry")
						{
							goto Label_008E;
						}
						if (str3 == "UID")
						{
							goto Label_0097;
						}
						if (str3 == "Scope")
						{
							goto Label_00A0;
						}
					}
					else
					{
						this.AccessToken = grantedOffers;
					}
				}
				continue;
			Label_0085:
				this.RefreshToken = grantedOffers;
				continue;
			Label_008E:
				this.Expiry = grantedOffers;
				continue;
			Label_0097:
				this.UserId = grantedOffers;
				continue;
			Label_00A0:
				this.Scope = grantedOffers;
				this.GrantedOffers = WrapProtocolHelper.ParseOffers(grantedOffers);
			}
		}

		public AppAuthentication(string accessToken, string refreshToken)
		{
			this.AccessToken = accessToken;
			this.RefreshToken = refreshToken;
		}

		public IDictionary<string, string> ToDictionary()
		{
			Dictionary<string, string> dictionary = new Dictionary<string, string>();
			dictionary.Add("UID", this.UserId);
			dictionary.Add("Expiry", this.Expiry);
			dictionary.Add("AccessToken", this.AccessToken);
			dictionary.Add("RefreshToken", this.RefreshToken);
			dictionary.Add("Scope", this.Scope);
			return dictionary;
		}

		// Properties
		public string AccessToken { get; internal set; }
		public string ClientVerifier { get; set; }
		public Exception Error { get; internal set; }
		public DateTime ExpirationDate
		{
			get
			{
				long num;
				if (long.TryParse(this.Expiry, out num))
				{
					return AuthConstants.Wrap.BaseDateTime.AddSeconds((double)num);
				}
				return new DateTime();
			}
		}

		internal string Expiry { get; set; }
		public Collection<Offer> GrantedOffers { get; set; }
		public string RefreshToken { get; internal set; }
		public string Scope { get; set; }
		public string UserId { get; set; }

	}
}
